//
//  UIView+JSAlert.m
//  JSBasicFramework
//
//  Created by Apple on 2019/1/16.
//  Copyright © 2019 Apple. All rights reserved.
//

#import "UIView+JSAlert.h"
#import <MMMaterialDesignSpinner.h>

#define LoadingTag 888888


@implementation UIView (JSAlert)


- (void)showLoadingWithTip:(NSString *)tip{
    
    UIView *loadingView = [self viewWithTag:LoadingTag];
    if (loadingView) {
        return;
    }
    loadingView = [[UIView alloc] initWithFrame:self.bounds];
    loadingView.tag = LoadingTag;
    [self addSubview:loadingView];
    [loadingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    MMMaterialDesignSpinner *spinner = [MMMaterialDesignSpinner new];
    spinner.tintColor = JS_colorWithHex(kBodyColor);
    [spinner startAnimating];
    [loadingView addSubview:spinner];
    [spinner mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(loadingView);
        make.size.mas_equalTo(CGSizeMake(44, 44));
    }];
    [self bringSubviewToFront:loadingView];
    if (tip.length) {
        UILabel *tipLabel = [UILabel new];
        tipLabel.font = UIFontMake(12);
        tipLabel.textAlignment = NSTextAlignmentCenter;
        [loadingView addSubview:tipLabel];
        
    }
    
}
- (void)hidenLoading{
    __block UIView *loadingView = [self viewWithTag:LoadingTag];
    if (loadingView) {
        [UIView transitionWithView:self duration:0.3f options:UIViewAnimationOptionTransitionCrossDissolve animations:^(void) {
            [loadingView removeFromSuperview];
        } completion:^(BOOL finished) {
            loadingView = nil;
        }];
    }
}
/* 1.给UIView添加点击事件*/
- (void)addTarget:(id)target
           action:(SEL)action;
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:target
                                                                         action:action];
    self.userInteractionEnabled = YES;
    [self addGestureRecognizer:tap];
}

/// 基本数值试配  避免重复计算
- (CGFloat)space10{
    NSNumber *space = objc_getAssociatedObject(self, _cmd);
    if (!space) {
        space = [NSNumber numberWithFloat:KREALVALUE(10)];
        objc_setAssociatedObject(self, _cmd, space, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return [space floatValue];
}

- (CGFloat)space12{
    NSNumber *space = objc_getAssociatedObject(self, _cmd);
    if (!space) {
        space = [NSNumber numberWithFloat:KREALVALUE(12)];
        objc_setAssociatedObject(self, _cmd, space, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return [space floatValue];
}
- (CGFloat)space14{
    NSNumber *space = objc_getAssociatedObject(self, _cmd);
    if (!space) {
        space = [NSNumber numberWithFloat:KREALVALUE(14)];
        objc_setAssociatedObject(self, _cmd, space, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return [space floatValue];
}
- (CGFloat)space15{
    NSNumber *space = objc_getAssociatedObject(self, _cmd);
    if (!space) {
        space = [NSNumber numberWithFloat:KREALVALUE(15)];
        objc_setAssociatedObject(self, _cmd, space, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return [space floatValue];
}
- (CGFloat)space16{
    NSNumber *space = objc_getAssociatedObject(self, _cmd);
    if (!space) {
        space = [NSNumber numberWithFloat:KREALVALUE(16)];
        objc_setAssociatedObject(self, _cmd, space, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return [space floatValue];
}
- (CGFloat)space18{
    NSNumber *space = objc_getAssociatedObject(self, _cmd);
    if (!space) {
        space = [NSNumber numberWithFloat:KREALVALUE(18)];
        objc_setAssociatedObject(self, _cmd, space, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return [space floatValue];
}
- (CGFloat)space20{
    NSNumber *space = objc_getAssociatedObject(self, _cmd);
    if (!space) {
        space = [NSNumber numberWithFloat:KREALVALUE(20)];
        objc_setAssociatedObject(self, _cmd, space, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return [space floatValue];
}
- (void)startShakeCompletion:(dispatch_block_t)animFinish{
    // 晃动次数
    static int numberOfShakes = 4;
    // 晃动幅度（相对于总宽度）
    //    static float vigourOfShake = 0.04f;
    // 晃动延续时常（秒）
    static float durationOfShake = 0.5f;
    
    CAKeyframeAnimation *shakeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    [shakeAnimation setQmui_animationDidStartBlock:^(__kindof CAAnimation *aAnimation) {
        if (animFinish) {
            animFinish();
        }
    }];
    // 方法一：绘制路径
    CGRect frame = self.frame;
    // 创建路径
    CGMutablePathRef shakePath = CGPathCreateMutable();
    // 起始点
    CGPathMoveToPoint(shakePath, NULL, CGRectGetMidX(frame), CGRectGetMidY(frame));
    for (int index = 0; index < numberOfShakes; index++)
    {
        // 添加晃动路径,固定路径
        CGPathAddLineToPoint(shakePath, NULL, CGRectGetMidX(frame) - 20.0f,CGRectGetMidY(frame));
        CGPathAddLineToPoint(shakePath, NULL,  CGRectGetMidX(frame) + 20.0f,CGRectGetMidY(frame));
        
        //         // 添加晃动路径 幅度由大变小
        //         CGPathAddLineToPoint(shakePath, NULL, CGRectGetMidX(frame) - frame.size.width * vigourOfShake*(1-(float)index/numberOfShakes),CGRectGetMidY(frame));
        //         CGPathAddLineToPoint(shakePath, NULL,  CGRectGetMidX(frame) + frame.size.width * vigourOfShake*(1-(float)index/numberOfShakes),CGRectGetMidY(frame));
    }
    // 闭合
    CGPathCloseSubpath(shakePath);
    shakeAnimation.path = shakePath;
    shakeAnimation.duration = durationOfShake;
    // 释放
    CFRelease(shakePath);
    [self.layer addAnimation:shakeAnimation forKey:kCATransition];
    
    //    CABasicAnimation *shakeAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
    //    shakeAnimation.duration = 0.08;
    //    shakeAnimation.autoreverses = YES;
    //    shakeAnimation.repeatCount = 5;
    //    shakeAnimation.fromValue = [NSValue valueWithCGRect:CGRectMake(imageV.frame.origin.x - 50, imageV.frame.origin.y, imageV.frame.size.width, imageV.frame.size.height)];//[NSValue valueWithCATransform3D:CATransform3DRotate(imageV.layer.transform, -0.06, 0, 0, 1)];
    //    shakeAnimation.toValue = [NSValue valueWithCGRect:CGRectMake(imageV.frame.origin.x - 50, imageV.frame.origin.y, imageV.frame.size.width, imageV.frame.size.height)];//[NSValue valueWithCATransform3D:CATransform3DRotate(imageV.layer.transform, 0.06, 0, 0, 1)];
    //    [imageV.layer addAnimation:shakeAnimation forKey:@"shakeAnimation"];
}
@end
